<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable @clear="clearInput">
          <el-button slot="append" icon="el-icon-search" @click="keySearch"></el-button>
        </el-input>
        <el-button type="primary" @click="goaddpage">添加商品</el-button>
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column label="#" type="index" width="50px"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格（元）" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="50px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="170px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removegoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodstotal">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  created () {
    this.getGoodsList()
  },
  data () {
    return {
      inputvalue: '',
      // 请求商品列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodsList: [],
      // 商品总数
      goodstotal: 0
    }
  },
  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.goodstotal = res.data.total
    },
    // 显示条数变化
    pageSizeChange (size) {
      this.queryInfo.pagesize = size
      this.getGoodsList()
    },
    // 显示页码变化
    pageCurrentChange (size) {
      this.queryInfo.pagenum = size
      this.getGoodsList()
    },
    // 搜索
    keySearch () {
      this.getGoodsList()
    },
    // 清空搜索框
    clearInput () {
      this.queryInfo.query = ''
      this.getGoodsList()
    },
    // 删除商品
    async removegoods (Id) {
      const data = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (data === 'cancel') return this.$message({ type: 'info', message: '已取消删除' })
      const { data: res } = await this.$http.delete(`goods/${Id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.getGoodsList()
      this.$message.success(res.meta.msg)
    },
    goaddpage () {
      this.$router.push('/goods/add')
    },
    // 添加商品
    addgoods () {
    }
  }
}
</script>
<style lang="less" scoped>
.input-with-select{
  width: 300px;
  margin-right: 30px;
}
.el-table{
  margin-top:20px;
}
</style>
